package wlsAgent;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

public class FusionSpyUtil {

	private static Logger log;

	static {
		log = Logger.getLogger(wlsAgent.FusionSpyUtil.class);
	}

	// convert string to Document

	public static Document string2Document(String s) {
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(s);
		} catch (Exception ex) {
			log
					.error(
							"Errors occured while converting string to org.dom4j.Document",
							ex);
		}
		return doc;
	}

	public static String bytesToString(byte[] bufferBytes) {
		int len = 0;
		if (bufferBytes != null) {
			len = bufferBytes.length;
		}
		return new String(bufferBytes, 0, len);
	}

	public static char[] bytesToChars(byte[] bytes) {
		String s = new String(bytes);
		char chars[] = s.toCharArray();
		return chars;
	}

	// Get current date (return yyyy-MM-dd HH:mm:ss)

	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static long showAsMeg(Long origNum) {
		return ((origNum.longValue() / 1024) / 1024);
	}

	public static void initLog4j(String log4jurl) {
		java.net.URL configFileUrl = ClassLoader.getSystemResource(log4jurl);
		PropertyConfigurator.configure(configFileUrl);
	}

	public static String longToDateStr(Long activationTime) {
		Calendar cal = Calendar.getInstance();
		Date date = cal.getTime();
		date.setTime(activationTime);
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDateTime = formater.format(date);
		return strDateTime;
	}

	public static String objectToString(Object obj) {
		if (obj instanceof Integer) {
			return String.valueOf(obj);
		} else if (obj instanceof String) {
			return (String) obj;
		} else if (obj instanceof Double) {
			return String.valueOf(obj);
		} else if (obj instanceof Float) {
			return String.valueOf(obj);
		} else if (obj instanceof Long) {
			return String.valueOf(obj);
		} else if (obj instanceof Boolean) {
			return String.valueOf(obj);
		} else if (obj instanceof Date) {
			return String.valueOf(obj);
		}
		return null;
	}

}
